﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BDH.PRE.Form 
{
    using BDH.DAL.Entities;
    public partial class FrmQuan : BDH.PRE.Form.FrmInput
    {
        BDH.BLL.QuanBLL _bll = new BDH.BLL.QuanBLL();
        public FrmQuan()
        {
            InitializeComponent();
            SetCaptionForm("Danh mục Quận / Huyện");

            btnSelect.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnPrint.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ClearDataBindings();
            ReadOnlyControl(false);
            ChangeStatus(false);
            ResetText();

            txtMa.EditValue = CreateID("Quan", "ma", "QH", 2);
            txtTen.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var _id = Convert.ToInt32(gvQuan.GetFocusedRowCellValue("id"));

            DialogResult result;
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                var ok = _bll.Delete(_id);
                if (ok)
                {
                    PerformRefresh();
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtTen.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtTen.Text);

            if (IsAdd)
            {
                var o = new Quan()
                {
                    ma = "" + txtMa.EditValue,
                    ten = name
                };

                var oki = _bll.Insert(o);
                if (oki)
                {
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(gvQuan.GetFocusedRowCellValue("id"));

                var o = new Quan()
                {
                    id = _id,
                    ten = name
                };

                var oki = _bll.Update(o);
                if (oki)
                {
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtMa.Text = null;
            txtTen.Text = null;
            
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMa.DataBindings.Clear();
            txtTen.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMa.DataBindings.Add("EditValue", _dtb, ".ma");
            txtTen.DataBindings.Add("EditValue", _dtb, ".ten");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtTen.Properties.ReadOnly = isReadOnly;
         
            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _bll.Select();

            if (_dtb != null)
                dgQuan.DataSource = _dtb;

            base.LoadData();
        }
        #endregion

    }
}
